import taichi as ti

ti.init(arch=ti.cuda)

n = 360

pixels = ti.Vector.field(3, ti.f32, shape=(n * 2, n))

# ppp = ti.Vector.field(3, ti.f32 ,shape=())
# pixels = ti.root.pointer(ti.ij, (n*2, n)).place(ppp)


@ti.kernel
def paint(t: float):
    for i, j in pixels:
        pixels[i, j] = [0, 0, 0]  #clear screen  清屏

        r = i * (2.2 / n)

        #此处不建议使用ti.cos(t*0.1) 取0-1 会出现后期高频振荡没有图像  不过动画有点好玩
        # z = 0.9 * ti.cos(t*0.1)  # interest one
        z = 0.9 * ti.cos(ti.random())

        # 同一个r尝试j次 获得j个收敛值
        # for xx in range(0,j):
        #     iterations = 0
        #     while iterations < 100:
        #         z = r*z*(1-z)
        #         iterations+=1

        #     jj = ti.cast(z*500.0 , ti.i32)
        #     pixels[i,jj]=[20, 0 ,90]

        iterations = 0
        while iterations < 10000:
            z = r * z * (1 - z)
            iterations += 1
        jj = ti.cast(z * n * 0.9, ti.i32)
        pixels[i, jj] = [20, 0, 90]


gui = ti.GUI("logistic  ", pixels.shape)

for i in range(1000000):
    # gui.clear(0x000000)
    paint(i * 0.03)
    gui.set_image(pixels)
    gui.show()
